package com.mxpio.crm.followup.controller;

import com.mxpio.crm.customer.entity.CrmCustomer;
import com.mxpio.crm.customer.service.ICrmCustomerService;
import com.mxpio.crm.followup.entity.CrmFollowupRecord;
import com.mxpio.crm.followup.service.ICrmFollowupRecordService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

/**
 * 跟进记录Controller
 *
 * @author Cheng
 * @date 2022-11-24
 */
@Slf4j
@Tag(name = "跟进记录Controller", description = "跟进记录")
@RestController()
@RequestMapping("/followup")
public class CrmFollowupRecordController {

    @Autowired
    private ICrmFollowupRecordService crmFollowupRecordService;
    @Autowired
    private ICrmCustomerService crmCustomerService;

    /**
     * 查询跟进记录列表
     */
    @GetMapping("/list")
    @Operation(summary = "跟进记录", description = "分页查询", method = "GET")
    public Result<Page<CrmFollowupRecord>> list(Criteria criteria,
                                         @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                         @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo) throws Exception {
        Pageable pageAble = PageRequest.of(pageNo - 1, pageSize);
        Page<CrmFollowupRecord> page = crmFollowupRecordService.page(criteria, pageAble);
        return Result.OK(page);
    }

    /**
     * 添加
     */
    @PostMapping(value = "/add")
    @Operation(summary = "跟进记录-添加", description = "跟进记录-添加", method = "POST")
    public Result<?> add(@RequestBody CrmFollowupRecord crmFollowupRecord) {
//        if(!"crm_customer".equals(crmFollowupRecord.getCategory())){
//            return Result.error("跟进类型错误！");
//        }
        if("crm_customer".equals(crmFollowupRecord.getCategory())){
            CrmCustomer customer = crmCustomerService.getById(crmFollowupRecord.getTypesId());
            if(customer != null){
                customer.setLastContent(crmFollowupRecord.getContent());
                crmCustomerService.update(customer);
            }
        }
        crmFollowupRecordService.save(crmFollowupRecord);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param crmFollowupRecord
     * @return
     */
    @Operation(summary = "跟进记录-编辑", description = "跟进记录-编辑", method = "PUT")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody CrmFollowupRecord crmFollowupRecord) {
        crmFollowupRecordService.update(crmFollowupRecord);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete")
    @Operation(summary = "跟进记录-通过id删除", description = "跟进记录-通过id删除")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        crmFollowupRecordService.delete(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/deleteBatch")
    @Operation(summary = "跟进记录-批量删除", description = "跟进记录-批量删除")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/queryById")
    @Operation(summary = "跟进记录-通过id查询", description = "跟进记录-通过id查询")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        CrmFollowupRecord crmFollowupRecord = crmFollowupRecordService.getById(id);
        if (crmFollowupRecord == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(crmFollowupRecord);
    }


}
